<?php

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

class User extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';
    protected static function init(){
     echo '初始化';
 }
    protected $schema=[
        'id'=>'int',
        'name'=>'string',
        'age'=>'int',
        'gender'=>'string',
        'details'=>'string',
        'create_time'=>'datetime',
        'update_time'=>'datetime'
    ];
//废弃字段，这样就不会查询给用户了
// protected  $disuse=['age','gender'];

// 只读字段
protected  $readonly=['age','name'];

public function getStatusAttr($value, $data)
{
   $status = [-1=>"删除", 0=>"冻结", 1=>"正常", 2=>"待审核"];
   return $status[$value];
}

    public function setAgeAttr($value)
    {
        return $value  +100;
}

//名称搜索器的模型方法
    public function searchNameAttr($query, $value, $data)
    {
//        dump($data);
        $query->where("name", "like", "%".$value."%");
        if (isset($data['sort'])){
            $query->order($data['sort']);
        }
    }

// 搜索器，限定时间
    public function searchCreateTimeAttr($query, $value, $data)
    {
        $query->whereBetweenTime("create_time", $value[0], $value[1]);
        if (isset($data['sort'])){
            $query->order($data['sort']);
        }
    }
}